package cn.source.web.controller.api;

import cn.source.common.annotation.RepeatSubmit;
import cn.source.common.constant.HttpStatus;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.utils.StringUtils;
import cn.source.system.domain.HouseRoom;
import cn.source.system.service.IHouseRoomService;
import com.alibaba.fastjson2.JSONObject;
import javafx.scene.control.Tab;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/room")
public class RoomController extends BaseController {

    @Resource
    private IHouseRoomService houseRoomService;

    /**
     * @Description: 获取房源列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseRoom houseRoom) {
        startPage();
        List<HouseRoom> list = houseRoomService.findHouseRoomList(houseRoom);
        return getDataTable(list);
    }

    /**
     * @Description: 获取用户的房源列表
     */
    @GetMapping("/getUserHouse")
    public TableDataInfo getUserHouse(HouseRoom houseRoom){
        startPage();
        List<HouseRoom> list = houseRoomService.getUserHouse(houseRoom);
        return getDataTable(list);
    }

    /**
     * @Description: 添加房源信息
     */
    @RepeatSubmit()
    @PostMapping("/add")
    public AjaxResult addHouse(@RequestBody HouseRoom houseRoom) {
        houseRoomService.apiInsertHouseRoom(houseRoom);
        return AjaxResult.success();
    }

    /**
     * @Description: 修改房源信息
     */
    @RepeatSubmit()
    @PostMapping("/update")
    public AjaxResult updateHouse(@RequestBody HouseRoom houseRoom) {
        String msg = "房源修改成功";
        houseRoomService.updateHouseRoom(houseRoom);
        return AjaxResult.success();
    }

    /**
     * @Description: 房源下架
     */
    @RepeatSubmit()
    @PutMapping("/offline")
    public AjaxResult offlineHouse(@RequestBody HouseRoom houseRoom){
        return AjaxResult.success(houseRoomService.offlineHouse(houseRoom));
    }



    /**
     * @Description: 获取房源信息by id
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        String msg = "房源查找成功";
        HouseRoom houseRoom = houseRoomService.selectHouseRoomById(id);
        houseRoom = houseRoomService.selectDetailHouseRoom(houseRoom);
        AjaxResult ajaxResult = new AjaxResult(HttpStatus.SUCCESS, msg, houseRoom);
        return ajaxResult;
    }


}
